package ce.com.newbluedemo.k2;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import ce.com.cenewbluesdk.proxy.BleFactory;
import ce.com.cenewbluesdk.proxy.CEDevK2Proxy;
import ce.com.newbluedemo.R;
import ce.com.newbluedemo.my.MainActivity;

import static ce.com.cenewbluesdk.entity.k2.K2_Action.SEND_R.SEND_TIME;

/**
 * Created by YaoBinkun on 2016/12/28.
 */

public class ActivityBluetoothTest extends K2BaseActivity implements View.OnClickListener {
    private Button mBtnCheckConnState;
    private Button mBtnConn;
    private Button mBtnSaveBleAddr;
    private Button mBtnShowSavedBleAddr;
    private Button mBtnSendTime;
    private TextView mTvConnState;
    private TextView mTvSavedBleAddr;
    private CEDevK2Proxy mK2Proxy;
    private Handler mK2Handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    showToast("蓝牙已断开");
                    break;
                case 1:
                    showToast("蓝牙连接成功");
                    break;

                default:
                    break;
            }
            if (msg.what == SEND_TIME.hashCode()) {
                if (msg.arg1 == 0) showToast("发送时间成功！");
            }


        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_layout);
        setTitle("蓝牙测试");
        mK2Proxy = BleFactory.getInstance().getK2Proxy();//获取K2蓝牙代理
        mK2Proxy.setHandler(mK2Handler);
        mBtnCheckConnState = (Button) findViewById(R.id.btn1);
        mBtnCheckConnState.setVisibility(View.VISIBLE);
        mBtnCheckConnState.setOnClickListener(this);
        mBtnConn = (Button) findViewById(R.id.btn2);
        mBtnConn.setVisibility(View.VISIBLE);
        mBtnConn.setOnClickListener(this);
        mBtnSaveBleAddr = (Button) findViewById(R.id.btn3);
        mBtnSaveBleAddr.setVisibility(View.VISIBLE);
        mBtnSaveBleAddr.setOnClickListener(this);
        mBtnShowSavedBleAddr = (Button) findViewById(R.id.btn4);
        mBtnShowSavedBleAddr.setVisibility(View.VISIBLE);
        mBtnShowSavedBleAddr.setOnClickListener(this);
        mBtnSendTime = (Button) findViewById(R.id.btn5);
        mBtnSendTime.setVisibility(View.VISIBLE);
        mBtnSendTime.setOnClickListener(this);
        findViewById(R.id.state1).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.text1_1)).setText("蓝牙连接状态：");
        mTvConnState = (TextView) findViewById(R.id.text1_2);
        findViewById(R.id.state4).setVisibility(View.VISIBLE);
        ((TextView) findViewById(R.id.text4_1)).setText("已保存的蓝牙地址：");
        mTvSavedBleAddr = (TextView) findViewById(R.id.text4_2);
    }

    @Override
    public void onClick(View v) {
        if (mK2Proxy.isDisconnect()) {
            showToast("蓝牙已断开，请重新连接");
            return;
        }
        switch (v.getId()) {
            case R.id.btn1://check bluetooth connect state
                mTvConnState.setText(mK2Proxy.isConnectOK() ? "Connected" : "Disconnected");
                showToast(mK2Proxy.isConnectOK() ? "Connected" : "Disconnected");
                break;


            case R.id.btn2://connect bluetooth
                if (mK2Proxy.getBlueAddress() != null) {
                    mK2Proxy.connectDev(mK2Proxy.getBlueAddress());
                    showToast("连接成功");
                } else {
                    Intent intent = new Intent(this, MainActivity.class);//扫描并选择蓝牙设备界面
                    startActivity(intent);
                }
                break;
            case R.id.btn3://Save Bluetooth address
                GlobalVar.SAVED_BLE_ADDR = mK2Proxy.getBlueAddress();
                mK2Proxy.setBlueAddress(mK2Proxy.getBlueAddress());
                showToast("success, bluetooth address saved");
                break;
            case R.id.btn4: //show saved bluetooth address
                mTvSavedBleAddr.setText(TextUtils.isEmpty(GlobalVar.SAVED_BLE_ADDR) ? "no address saved yet" : GlobalVar.SAVED_BLE_ADDR);
                break;
            case R.id.btn5: //send time
                mK2Proxy.getSendHelper().sendTime();
                showToast("send time successfully");
                break;
            case R.id.btn6:
                mK2Proxy.disConnect();
                break;
            default:
                break;
        }

    }

    private void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }
}
